package com.grandtech.insurance.common.http;

import com.google.gson.Gson;
import com.grandtech.insurance.common.utils.GsonFactory;

import java.io.Serializable;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

/**
 * Created by zy on 2018/8/15.
 */

public class BaseAppHeader implements Serializable {

    /**
     * app名称
     */
    private String appName;
    /**
     * app版本名称
     */
    private String appVersionName;
    /**
     * app版本号
     */
    private String appVersionCode;
    /**
     * 手机设备号
     */
    private String imei;
    /**
     * 请求时间（移动端）
     */
    private Date dateTime = new Date();

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getAppVersionName() {
        return appVersionName;
    }

    public void setAppVersionName(String appVersionName) {
        this.appVersionName = appVersionName;
    }

    public String getAppVersionCode() {
        return appVersionCode;
    }

    public void setAppVersionCode(String appVersionCode) {
        this.appVersionCode = appVersionCode;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public Date getDateTime() {
        return dateTime;
    }

    public String encode() {
        if (this == null) return null;
        Gson gson = GsonFactory.getFactory().getComGson();
        String json = gson.toJson(this);
        try {
            return URLEncoder.encode(json, "UTF-8");
        } catch (Exception e) {
            return null;
        }
    }


    public static ComHeader decode(String o) {
        try {
            Gson gson = GsonFactory.getFactory().getComGson();
            String json = URLDecoder.decode(o, "UTF-8");
            ComHeader comHeader = gson.fromJson(json, ComHeader.class);
            if (comHeader.getCompanyName() == null
                    || comHeader.getCompanyName().equals("")
                    || comHeader.getCompanyCode() == null
                    || comHeader.getCompanyCode().equals("")) {
                return null;
            }
            return comHeader;
        } catch (Exception e) {
            return null;
        }
    }
}
